package csnet.openflow.util.log;

import java.io.IOException;
import java.util.HashMap;

import csnet.openflow.util.fileOperation.FileWriter;
import csnet.openflow.util.fileOperation.StreamWriter;

public class LoggerGenerator {
    private String basePath;
    private HashMap<String, FileWriter> textLoggers = new HashMap<>();
    private HashMap<String, StreamWriter> binaryLoggers = new HashMap<>();

    public LoggerGenerator(String basePath) {
        textLoggers = new HashMap<>();
        binaryLoggers = new HashMap<>();
        this.basePath = basePath;
    }

    public FileWriter getTextLogger(String name) {
        try {
            FileWriter writer = new FileWriter(basePath + name, false);
            textLoggers.put(name, writer);
            return writer;
        } catch (IOException e) {
            return null;
        }
    }

    public StreamWriter getBinaryLogger(String name) {
        try {
            StreamWriter writer = new StreamWriter(basePath + name, false);
            binaryLoggers.put(name, writer);
            return writer;
        } catch (IOException e) {
            return null;
        }
    }

    public void closeAll() {
        for (FileWriter writer : textLoggers.values()) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for (StreamWriter writer : binaryLoggers.values()) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
